package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.Md5;
import com.ljfiction.book.tools.PreferencesTool;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class FindPasswordnextActivity extends BaseActivity {

    @BindView(R.id.back_findnext)
    ImageView backFindnext;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.passwordeye)
    ImageView passwordeye;
    @BindView(R.id.confirmpassword)
    EditText confirmpassword;
    @BindView(R.id.confirmpasswordeye)
    ImageView confirmpasswordeye;
    @BindView(R.id.next_findok)
    SuperButton nextFindok;

    private boolean isCheckeye = false;
    private boolean isCheckeyetow = false;
    private String phoneId;
    private String code;
    private NoBackgLodDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_passwordnext);
        ButterKnife.bind(this);
        phoneId = getIntent().getStringExtra("phoneId");
        code=getIntent().getStringExtra("code");
    }

    @OnClick({R.id.back_findnext, R.id.passwordeye, R.id.confirmpasswordeye, R.id.next_findok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_findnext:
                goBack();
                break;
            case R.id.passwordeye:
                if (isCheckeye) {
                    isCheckeye = false;
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    passwordeye.setImageResource(R.mipmap.eyebihui);

                } else {
                    isCheckeye = true;
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    passwordeye.setImageResource(R.mipmap.eyekaihui);
                }
                break;
            case R.id.confirmpasswordeye:
                if (isCheckeyetow) {
                    isCheckeyetow = false;
                    confirmpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    confirmpasswordeye.setImageResource(R.mipmap.eyebihui);
                } else {
                    isCheckeyetow = true;
                    confirmpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    confirmpasswordeye.setImageResource(R.mipmap.eyekaihui);
                }
                break;
            case R.id.next_findok:
                if ("".equals(phoneId)) {
                    showToast("请返回重新输入手机号");
                } else if ("".equals(password.getText().toString())) {
                    showToast("请输入新密码");
                }else if (!StringTool.isPass(password.getText().toString())) {
                    showToast(StringTool.isPassString(password.getText().toString()));
                    return;
                }  else if ("".equals(confirmpassword.getText().toString())) {
                    showToast("请确认新密码");
                } else if (!password.getText().toString().equals(confirmpassword.getText().toString())) {
                    showToast("请输入同一个密码");
                } else {
                    tofindmima();
                }
                break;
        }
    }

    private void tofindmima() {
        OkHttpUtils.post(HttpManager.IP + "/person-pwdRetrieval")

                .params("newPwd", Md5.encryption(password.getText().toString()))
                .params("code",code)
                .params("phoneId",phoneId)
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (loadingDialog==null){
                            loadingDialog=new NoBackgLodDialog(FindPasswordnextActivity.this,true);
                        }
                        loadingDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (loadingDialog!=null){
                            loadingDialog.dismis();
                        }
                        try {
                            JSONObject js=new JSONObject(s);
                            if (js.getInt("returnCode")==200){
                                PreferencesTool.saveLoginInfo(FindPasswordnextActivity.this, phoneId, password.getText().toString());
                                setResult(2);
                                showToast(js.getString("msg"));
                                goBack();
                            }else {
                                showToast(js.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

    }
}
